$(function () {
    var goods_id = parseInt(GetQueryString('goods_id'));
    var html_data = {
        baseConfig: baseConfig,
        sessionid: '',
        sessionid_q: '',
        show_share_bar: false,
        show_pengyou_dongtai_images: false,
        user_id: 0,
        goods_info: {},
        together_goods_info: {},
        goods: {files_list: [], buy_user_list: []},
        comment_list: [],
        value_list: [],
        start_list: [{type: 1, index: 0}, {type: 1, index: 1}, {type: 1, index: 2}, {type: 1, index: 3}, {
            type: 1,
            index: 4
        }],
        selected_img:"http://www.mmggoo.com/static/weixin/img2/icon.png",
        goods_order_item: []

    };
    var app = new Vue({
        el: '#address_modefy',
        data: html_data,
        methods: {
            price_filter: price_filter,
            ping_lun: function (goods, comment) {
                var title = "评论";
                var to_user_id = "";
                if (comment) {
                    title = "对 " + comment.user__realname + " 说:"
                    to_user_id = comment.user_id;
                }
                Magiclamp.mcPromptPingLun(title, "", function (content) {
                    api.lgc.create_goods_comment({
                        goods_id: goods.id,
                        content: content,
                        to_user_id: to_user_id
                    }).then(function (data) {
                        Magiclamp.mcAlert("提示", "评论成功");
                        query_info();
                    })
                });
            },
            first_name: function (realname) {
                var title = realname.substring(0, 1);
                for (var i = 0; i < realname.length; i++) {
                    title += "*"
                }
                return title;
            },
            buy_date_ts: function (info) {

                var realname = info.user__realname;
                if (realname == undefined) {
                    realname = info.goods_order__user__realname;
                }
                var title = realname.substring(0, 1);
                for (var i = 0; i < realname.length; i++) {
                    title += "*"
                }

                Magiclamp.mcAlert("提示", title + "于" + info.create_time.substring(0, 10) + "购买。");
            },
            delete_goods_comment: function (info) {

                api.lgc.delete_goods_order_comment({goods_comment_id: info.id}).then(function (value) {
                    query_info();
                    Magiclamp.mcAlert("提示", "评论已删除");
                })
            },
            delete_friend_comment: function (info) {

                api.lgc.delete_goods_friend_comment({goods_comment_id: info.id}).then(function (value) {
                    query_info();
                    Magiclamp.mcAlert("提示", "评论已删除");
                })
            },

            show_big_pengyou_img: function (item, fileurl) {
                // program_data.show_pengyou_dongtai = false;
                html_data.show_pengyou_dongtai_images = true;
                var width = window.innerWidth
                    || document.documentElement.clientWidth
                    || document.body.clientWidth;
                var height = window.innerHeight
                    || document.documentElement.clientHeight
                    || document.body.clientHeight;
                setTimeout(function () {
                    var pswpElement = document.getElementById("all_pengyou_pswp");

                    var items = [];
                    var index = 0;

                    _(item.files_json).each(function (sub, i) {
                        items.push({
                            src: baseConfig.qn_host + sub.fileurl + '?imageView2/0/w/' + width + '/h/' + (width * 4 / 3),
                            w: width,
                            h: (width * 4 / 3)
                        })
                        if (sub.fileurl == fileurl) {
                            index = i;
                        }
                    })

                    var options = {
                        // optionName: 'option value'
                        // for example:
                        mainClass: 'pswp--minimal--dark',
                        index: index, // start at first slide

                    };

                    var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
                    gallery.listen('close', function () {
                        // program_data.show_pengyou_dongtai = true;
                        html_data.show_pengyou_dongtai_images = false;
                    });
                    gallery.init();
                }, 200);

            },

            open_pro_user: function (user_id) {
                location.href = './pro_user_goods.html?user_id=' + user_id;
            },
            open_goods_detail: function (goods) {
                location.href = './goods.html?goods_id=' + goods.id;
            },

            get_fengmian: function (img_url1, img_url2) {
                if (img_url1) {
                    return baseConfig.qn_host + img_url1 + '?imageView2/1/w/270/h/270';
                } else if (img_url2) {
                    return baseConfig.qn_host + img_url2 + '?imageView2/1/w/270/h/270';
                }
            },
            page_back: function () {
                baseConfig.page_back();
            }
        }
    });

    html_data.sessionid = GetQueryString('sessionid');
    if (html_data.sessionid) {
        html_data.sessionid_q = "sessionid=" + html_data.sessionid;
        client_storage('sessionid', html_data.sessionid);
    }

    function query_info() {
        api.an.get_firend_buy_one_goods({goods_id: goods_id}).then(function (data) {

            html_data.user_id = data.result.user_id;
            html_data.goods_info = data.result.goods;
            html_data.goods = data.result.goods;


            try {
                html_data.goods.files_list = JSON.parse(html_data.goods.files_json);
                if (html_data.goods.files_list.length > 4) {
                    html_data.goods.files_list.length = 4;
                }
                html_data.selected_img = html_data.goods.files_list[0]['url']
            } catch (e) {

            }

            _(html_data.goods.pro_list).each(function (item) {
                try {
                    if (item.file_type == 1) {
                        item.files_json = JSON.parse(item.files_json);
                    } else if (item.file_type == 2) {
                        item.video_obj = JSON.parse(item.videos_json);
                    }
                } catch (e) {

                }
            })


        });
    }

    if (goods_id) {
        query_info();

    }

    login_mogu(function () {
        html_data.is_login = true;
        var share_id = GetQueryString("share_id");
        var share_user_id = GetQueryString("share_user_id");
        var from = GetQueryString("frompage");

        if (from == "news_share_link" || from == "goods_share_link") {
            var from_user_id = 0;
            if (share_user_id && share_user_id.length > 0) {
                from_user_id = share_user_id;
            }
            api.an.save_goods_clicked({
                goods_id: GetQueryString("goods_id"),
                from_user_id: from_user_id
            }).then(function () {
                console.log("保存链接进入记录")
            })
        }


        if (share_id && share_id.length > 0) {
            api.sys.set_share_user({i: share_id}).then(function (value) {
            })
        } else {
            if (share_user_id && share_user_id.length > 0) {
                api.sys.set_share_user_by_user({from_user_id: share_user_id}).then(function (value) {
                })
            }
        }

        api.sys.wx_js_sdk_info({url: location.href}).then(function (data) {
            api.sys.create_sharelink({ goods_id: GetQueryString("goods_id"), page: "goods_friend_comment_list.html" }).then(function (value) {
                api.lgoods.create_together_price_share({goods_id: GetQueryString("goods_id")}).then(function (together_value) {
                    var url = value.result.url + "&frompage=goods_share_link&together_id=" + together_value.result.together_id;
                    try {

                        wx.config({
                            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                            appId: 'wxd78bb63115a11db4', // 必填，公众号的唯一标识
                            timestamp: data.result.timestamp, // 必填，生成签名的时间戳
                            nonceStr: data.result.noncestr, // 必填，生成签名的随机串
                            signature: data.result.signature,// 必填，签名
                            jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填，需要使用的JS接口列表
                        });
                        wx.error(function (res) {
                            // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
                            console.log(res);
                        });
                        wx.ready(function () {
                            wx_has_ready = true;
                            if (html_data.goods_info.id) {
                                // var img_url = html_data.selected_img;
                                // if (!img_url) {
                                //     img_url = 'http://www.mmggoo.com/static/weixin/img2/icon.png';
                                // }
                                wx.onMenuShareTimeline({
                                    title: '一起买，你能优惠'+price_filter(html_data.goods_info.x_price)+'元，我也能再省'+price_filter(html_data.goods_info.y_price)+'元', // 分享标题
                                    link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                    imgUrl: html_data.selected_img, // 分享图标
                                    success: function (res) {
                                        // 用户确认分享后执行的回调函数
                                        api.an.save_goods_shared({goods_id: html_data.goods_info.id}).then(function () {
                                            console.log("记录分享商品链接成功")
                                        });
                                        console.log(res);
                                    },
                                    cancel: function (res) {
                                        // 用户取消分享后执行的回调函数
                                        console.log(res);
                                    }
                                });
                                wx.onMenuShareAppMessage({
                                    title: '华迅直购--' + html_data.goods_info.name, // 分享标题
                                    desc: '分享给你，一起买，你能优惠'+price_filter(html_data.goods_info.x_price)+'元，我也能再省'+price_filter(html_data.goods_info.y_price)+'元', // 分享描述
                                    link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                                    imgUrl: html_data.selected_img, // 分享图标
                                    type: '', // 分享类型,music、video或link，不填默认为link
                                    dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                                    success: function (res) {
                                        // 用户确认分享后执行的回调函数
                                        api.an.save_goods_shared({goods_id: html_data.goods_info.id}).then(function () {
                                            console.log("记录分享商品链接成功")
                                        });
                                        console.log(res);
                                    },
                                    cancel: function (res) {
                                        // 用户取消分享后执行的回调函数
                                        console.log(res);
                                    }
                                });


                            }

                        });


                    } catch (res) {
                        console.log(res);
                    }
                })
            });

        })
        var together_id = GetQueryString("together_id");
        if(together_id){
            api.lgoods.check_together_price_share({together_id:together_id}).then(function (check_value) {
                if(!check_value.result.has_visit){
                    api.lgoods.get_together_price_detail_info({together_id:together_id}).then(function (value) {
                        if(value.result.has_join){
                            ger_together_info_detail();
                            return;
                        }
                        var msg = "<span style='color: red;font-weight: bold'>"+value.result.buyer__realname+"</span>约您一起买，一起减。<br/>";
                        msg += "你能优惠"+"<span style='color: red;font-weight: bold'>"+price_filter(value.result.goods__x_price)+"元</span>，我也能再省";
                        msg += "<span style='color: red;font-weight: bold'>"+price_filter(value.result.goods__z_price)+"元</span>。你愿意一起买吗？";
                        Magiclamp.mcConfirm5("有人约你", msg, function () {
                            ger_together_info_detail();
                        }, function () {
                            // 调用参与
                            api.lgoods.join_together_price_share({together_id:together_id}).then(function (join_value) {
                                ger_together_info_detail();
                            })
                        })
                    })
                }else{
                    ger_together_info_detail();
                }
            })

        }else{
            ger_together_info_detail();
        }

    }, "goods", GetQueryString("goods_id"), true);

    function ger_together_info_detail() {
        api.lgoods.get_together_price_share_info({goods_id:GetQueryString("goods_id")}).then(function (value) {
            html_data.together_goods_info = value.result;
        })
    }


});
